<template>
  <div>
    <e-table
      ref="eTable"
      @getList="handleGetList"
      :tableData="tableData"
      :columns="columns"
      :loading="loading"
      :total="total"
    >
      <el-table-column slot="info">
        <span slot-scope="scope">
          <el-button type="text">{{ scope.row.supplierName }}</el-button>
        </span>
      </el-table-column>

      <el-table-column slot="idcard" label="身份证号" align="center">
        <span slot-scope="scope">
          <span v-if="scope.row.idcard !== ''">{{ scope.row.idcard }}</span>
          <span v-else>未识别</span>
        </span>
      </el-table-column>

      <el-table-column slot="idcardState" label="身份证状态" align="center" width="100">
        <span slot-scope="scope">
          <template v-if="scope.row.idcardFront">
            <el-button
              type="text"
              @click="handlePreview(scope.row, 'idcard')"
            >{{ scope.row.idcardEnd }}</el-button>
          </template>
          <template v-else>
            <span>未上传</span>
          </template>
        </span>
      </el-table-column>
      <el-table-column slot="qualification" label="购销资格证" align="center" width="110">
        <span slot-scope="scope">
          <el-button
            v-if="scope.row.qualification"
            type="text"
            @click="handlePreview(scope.row, 'qualification')"
          >{{ scope.row.qualificationEnd }}</el-button>
          <span v-else>未上传</span>
        </span>
      </el-table-column>
      <!-- <el-table-column slot="salesmanFlag" label="业务员" align="center" width="80">
        <span slot-scope="scope">
          <span v-if="scope.row.salesmanFlag === 1">是</span>
          <span v-else>否</span>
        </span>
      </el-table-column>-->
      <el-table-column slot="action" fixed="right" label="操作" width="50" align="center">
        <template slot-scope="scope">
          <el-dropdown>
            <a class="el-dropdown-link">
              <i class="el-icon-menu"></i>
            </a>
            <el-dropdown-menu slot="dropdown">
              <template v-if="!scope.row.handleStateName">
                <el-dropdown-item @click.native="handleEdit(scope.row)">
                  <a>编辑</a>
                </el-dropdown-item>
              </template>
              <el-dropdown-item @click.native="handleDelete(scope.row)">
                <a class="red" @click="handleDelete(scope.row)">删除</a>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </e-table>
    <preview-modal ref="previewModal" />
  </div>
</template>

<script>
import PreviewModal from '@/components/PreviewModal'
import { linkmanList, deleteLinkMan, linkmanDetail, getOneUrl } from '@/api/supplier'
import { getSession } from '@/utils/util'

export default {
  components: {
    PreviewModal
  },
  data() {
    return {
      queryParam: {
        pageNo: 1,
        pageSize: 10
      },
      loading: false,
      // 表头
      columns: [
        {
          label: '姓名',
          prop: 'linkmanName'
        },
        {
          label: '头衔',
          prop: 'linkmanTitle'
        },
        {
          label: '联系方式',
          width: '120px',
          prop: 'linkmanTel'
        },
        {
          slot: 'idcard'
        },
        {
          slot: 'idcardState'
        },
        // {
        //   slot: 'salesmanFlag'
        // },
        {
          slot: 'qualification'
        },
        {
          slot: 'action'
        }
      ],
      total: 1,
      tableData: [],
      mdl: {}
    }
  },
  mounted() {
    if (this.$route.params.obj) {
      this.mdl = this.$route.params.obj
    } else {
      this.mdl = getSession('record')
    }
    linkmanDetail(this.mdl.supplierCode).then(res => {
      this.tableData = res.data.records
    })
  },
  methods: {
    handleAdd() {
      this.$nextTick(() => {
        this.$emit('edit', false, {})
      })
    },
    // 预览图片处理
    handlePreview(record, type) {
      let id = ''
      if (type === 'idcard') {
        id = record.idcardFront
      } else {
        id = record.qualification
      }
      getOneUrl(id).then(res => {
        console.log('res', res)
        this.$refs.previewModal.show([res.data.id])
      })
    },
    handleEdit(row) {
      this.$nextTick(() => {
        this.$emit('edit', true, row)
      })
    },
    handleDelete(row) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除[${row.linkmanName}]这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await deleteLinkMan(row.linkmanId, {})
          openNotification('success', '删除成功')
          _this.handleGetList()
        }
      })
    },
    async handleGetList(param = {}) {
      this.loading = true
      this.queryParam = Object.assign(this.queryParam, param)
      const res = await linkmanList(this.queryParam)
      if (res && res.data.records.length > 0) {
        const {
          state,
          data: { records, total }
        } = res
        this.tableData = records
        this.total = total
      } else {
        this.tableData = []
        this.total = 1
      }
      this.loading = false
    }
  }
}
</script>

